package com.example.maryttsspringbootdemo.controller;

import com.example.maryttsspringbootdemo.service.MaryTtsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
public class TtsController {

    @Autowired
    private MaryTtsService ttsService;

    @PostMapping("/tts")
    public ResponseEntity<byte[]> generateSpeech(@RequestBody String text) throws IOException {
        try {
            byte[] audioBytes = ttsService.textToSpeech(text);

            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            headers.setContentDispositionFormData("file", "audio.wav");

            return ResponseEntity.ok()
                    .headers(headers)
                    .body(audioBytes);

        } catch (Exception e) {
            throw new IOException("语音合成失败: " + e.getMessage());
        }
    }
}